var EmployeeGroupModule = function() {
	var editable = function() {
		return {
			init: function() {
				$('#queryBtn').click(function() {
					oTable.fnClearTable();
				});
				// 删除
				$('#delBtn').click(
					function(e) {
						e.preventDefault();
						var groupId = $("#groupId").val();
						if (groupId == '' || groupId == undefined || groupId == null) {
							bootbox.alert("请选择一个删除的节点");
						} else {
							bootbox.confirm(
								"您确定删除该部门以及下面的部门?",
								function(result) {
									if (result) {
										$.ajax({
											type: "post",
											url: ctx + "/employeeGroup/delete",
											data: "groupId=" + groupId,
											dataType: "json",
											success: function(msg) {
												if (msg.code == 1) {
													bootbox.dialog({
														message: "删除成功！",
														title: "温馨提示：",
														buttons: {
															main: {
																label: "确定",
																className: "btn btn-primary",
																callback: function() {
																	window.location.reload();
																}
															}
														}
													});
												} else {
													bootbox.dialog({
														message: msg.message,
														title: "温馨提示：",
														buttons: {
															main: {
																label: "确定",
																className: "btn btn-primary",
																callback: function() {
																	window.location.reload();
																}
															}
														}
													});
												}
											},
											error: function(msg) {
												bootbox.dialog({
													message: "删除失败！",
													title: "温馨提示：",
													buttons: {
														main: {
															label: "确定",
															className: "btn btn-primary",
															callback: function() {
																window.location.reload();
															}
														}
													}
												});
											}
										});
									}
								});
						}
					});
				// 添加
				$('#addBtn').click(
					function(e) {
						e.preventDefault();
						var groupId = $("#groupId").val();
						if (groupId == '' || groupId == undefined || groupId == null) {
							bootbox.alert("请选择一个父节点");
						} else {
							$.ajax({
								type: "post",
								url: ctx + "/employeeGroup/checkGroupId",
								data: {
									"groupId": groupId
								},
								success: function(msg) {
									if (msg.code == 1) {
										$("#tags").val("");
										$("#groupName").val("");
										$("#employeeId").val("");
										$('#employeeName').html("");
										$('#employeeNo').html("");

										$('#addModel').modal('show');
									} else {
										bootbox.dialog({
											message: msg.message,
											title: "温馨提示：",
											buttons: {
												main: {
													label: "确定",
													className: "btn btn-primary",
													callback: function() {

													}
												}
											}
										});
									}
								}
							});
						}
					});
				// 编辑
				$('#editBtn').click(function() {
					var id = $("#groupId").val();
					if (id == '' || id == undefined || id == null || id == "root") {
						bootbox.alert("请选择一个节点");
					} else {
						$.ajax({
							type: "post",
							url: ctx + "/employeeGroup/get",
							data: "id=" + id,
							dataType: "json",
							success: function(msg) {
								if (msg.code == 1) {
									$("#tagsUpdate").val("");
									$("#groupNameUpdate").val("");
									$("#groupNameUpdate").val(msg.data.model.groupName);
									$("#employeeNameUpdate").html("");
									$("#employeeNameUpdate").html(msg.data.model.employeeName);
									if (msg.data.model.plannerGroup) {
										if (msg.data.model.saleAll) {
											$("#isAllowProduct2").val(1);
											$("#isAllowSellAllProduct2").show();
											$("#allowProduct2").hide();
										} else {
											$("#isAllowProduct2").val(0);
											$("#isAllowSellAllProduct2").show();
											$("#allowProduct2").show();
										}
										//根据id查找对象  
										var selGroupId2 = document.getElementById('selGroupId2');
										var unselGroupId2 = document.getElementById('unselGroupId2');

										//删除旧数据
										selGroupId2.options.length = 0;
										unselGroupId2.options.length = 0;

										//添加一个选项 
										var myProductList = msg.data.myProductList;
										var notMyProductList = msg.data.notMyProductList;

										for (var i = 0; i < myProductList.length; i++) {
											selGroupId2.options.add(new Option(myProductList[i].briefName, myProductList[i].id));
										}
										for (var i = 0; i < notMyProductList.length; i++) {
											unselGroupId2.options.add(new Option(notMyProductList[i].briefName, notMyProductList[i].id));
										}
									}else{
										$("#isAllowSellAllProduct2").hide();
										$("#allowProduct2").hide();
									}
									var modal = $('#editModel').modal('show');
									
									$("#updateGroupModel").unbind("click").click(function() {
										var groupId = $("#groupId").val();
										var groupName = $("#groupNameUpdate").val();
										var employeeId = $('#employeeIdUpdate').val();
										var employeeName = $('#employeeNameUpdate').html();
										var isAllowProduct = $("#isAllowProduct2").val();
										var se1 = $('#selGroupId2')[0];
										var lenth = se1.length;
										var productId = [];
										for (var i = 0; i < lenth; i++) {
											productId.push({
												"id": se1[i].value
											});
										}
										if (groupName == null || groupName == '') {
											bootbox.alert("请输入部门名称！");
											return;
										}
										var js = {
											"employeeId": employeeId,
											"employeeName": employeeName,
											"parent": groupId,
											"groupName": groupName,
											"saleAll": isAllowProduct,
											"saleProduct": productId
										};
										var json = JSON.stringify(js);
										$.ajax({
											type: "post",
											contentType: "application/json; charset=UTF-8",
											url: ctx + "/employeeGroup/update",
											data: json,
											dataType: "json",
											success: function(updateMsg) {
												if (updateMsg.code == 1) {
													bootbox.dialog({
														message: "更新部门信息成功！",
														title: "温馨提示：",
														buttons: {
															main: {
																label: "确定",
																className: "btn btn-primary",
																callback: function() {
																	window.location.reload();
																}
															}
														}
													});
												} else {
													bootbox.dialog({
														message: updateMsg.message,
														title: "温馨提示：",
														buttons: {
															main: {
																label: "确定",
																className: "btn btn-primary",
																callback: function() {
																	window.location.reload();
																}
															}
														}
													});
												}

											},
											error: function(updateMsg) {
												bootbox.dialog({
													message: "更新部门信息失败！",
													title: "温馨提示：",
													buttons: {
														main: {
															label: "确定",
															className: "btn btn-primary",
															callback: function() {
																window.location.reload();
															}
														}
													}
												});
											}
										});
									});

								} else {
									bootbox.dialog({
										message: msg.data.message,
										title: "温馨提示：",
										buttons: {
											main: {
												label: "确定",
												className: "btn btn-primary",
												callback: function() {
													window.location.reload();
												}
											}
										}
									});
								};
							},
							error: function(msg) {
								bootbox.dialog({
									message: "oh no,服务器出问题了!",
									title: "温馨提示：",
									buttons: {
										main: {
											label: "确定",
											className: "btn btn-primary",
											callback: function() {
												window.location.reload();
											}
										}
									}
								});
							}
						});
					}

				});

				/*
				 * $('#queryBtn').click(function() { oTable.fnClearTable(); });
				 */
				// Autocomplete
				$("#tags").autocomplete({
					source: function(query, process) {
						$.post(ctx + "/employeeGroup/employee/" + query.term, {
							"matchCount": 200
						}, function(data) {
							if (data.code == 1) {
								var ebean = [];
								for (var int = 0; int < data.data.length; int++) {
									var temp = [];
									temp.label = data.data[int].employeeName + "  " + data.data[int].employeeNo;
									temp.value = data.data[int].employeeNo;
									temp.id = data.data[int].employeeId;
									temp.name = data.data[int].employeeName;
									ebean.push(temp);
								}
								return process(ebean);
							}
						});
					},
					search: function(event, ui) {
						$("#employeeName").html('');
						$("#employeeNo").html('');
						$("#employeeId").val('');
					},
					select: function(event, ui) {
						$("#tags").val(ui.item.label);
						$("#employeeName").html(ui.item.name);
						$("#employeeNo").html(ui.item.value);
						$("#employeeId").val(ui.item.id);
						var uistr = ui.item;
						// 阻止事件的默认行为
						event.preventDefault();
					}
				});
				$("#tagsUpdate").autocomplete({
					source: function(query, process) {
						$.post(ctx + "/employeeGroup/employee/" + query.term, {
							"matchCount": 200
						}, function(data) {
							if (data.code == 1) {
								var ebean = [];
								for (var int = 0; int < data.data.length; int++) {
									var temp = [];
									temp.label = data.data[int].employeeName + "  " + data.data[int].employeeNo;
									temp.value = data.data[int].employeeNo;
									temp.id = data.data[int].employeeId;
									temp.name = data.data[int].employeeName;
									ebean.push(temp);
								}
								return process(ebean);
							}
						});
					},
					search: function(event, ui) {
						//$("#employeeNameUpdate").html('');
						// $("#employeeNoUpdate").html('');
						// $("#employeeIdUpdate").val('');
					},
					select: function(event, ui) {
						$("#tagsUpdate").val(ui.item.label);
						$("#employeeNameUpdate").html(ui.item.name);
						// $("#employeeNoUpdate").html(ui.item.value);
						$("#employeeIdUpdate").val(ui.item.id);
						var uistr = ui.item;
						// 阻止事件的默认行为
						event.preventDefault();
					}
				});

				$('#addGroupModel').click(function() {
					var parent = $("#groupId").val();
					var groupName = $("#groupName").val();
					var employeeId = $('#employeeId').val();
					var employeeName = $('#employeeName').html();
					var plannerGroup = $("#isPlannerGroup").val();
					var isAllowProduct = $("#isAllowProduct").val();
					var se1 = $('#selGroupId')[0];
					var lenth = se1.length;
					var productId = [];
					for (var i = 0; i < lenth; i++) {
						productId.push({
							"id": se1[i].value
						});
					}
					if (groupName == null || groupName == '') {
						bootbox.alert("请输入部门名称！");
						return;
					}
					if ((employeeId == null || employeeId == '')&&plannerGroup=="1") {
						bootbox.alert("请选择一个部门负责人！");
						return;
					}
					var js = {
						"employeeId": employeeId,
						"employeeName": employeeName,
						"parent": parent,
						"groupName": groupName,
						"plannerGroup": plannerGroup,
						"saleAll": isAllowProduct,
						"saleProduct": productId

					};
					var json = JSON.stringify(js);
					$.ajax({
						contentType: "application/json; charset=UTF-8",
						type: "post",
						url: ctx + "/employeeGroup/doAdd",
						data: json,
						success: function(msg) {
							if (msg.code == 1) {
								bootbox.dialog({
									message: "添加部门成功！",
									title: "温馨提示：",
									buttons: {
										main: {
											label: "确定",
											className: "btn btn-primary",
											callback: function() {
												window.location.reload();
											}
										}
									}
								});
							} else {
								bootbox.dialog({
									message: msg.message,
									title: "温馨提示：",
									buttons: {
										main: {
											label: "确定",
											className: "btn btn-primary",
											callback: function() {

											}
										}
									}
								});
							}
							/* window.location.href = ctx + "/news/list"; */
						},
						error: function(msg) {
							bootbox.dialog({
								message: "oh no,服务器出问题了!",
								title: "温馨提示：",
								buttons: {
									main: {
										label: "确定",
										className: "btn btn-primary",
										callback: function() {

										}
									}
								}
							});
						}
					});
				});

			}

		};

	}();
	var handleResourceTree = function() {
		var empTree;
		$('#resourceTree').jstree({
			'plugins': ["wholerow", "types"],
			'core': {
				"themes": {
					"responsive": false
				},
				'data': {
					"url": ctx + "/employeeGroup/tree"
				}
			},
			"types": {
				"default": {
					"icon": "fa fa-folder icon-warning icon-lg"
				},
				"file": {
					"icon": "fa fa-file icon-warning icon-lg"
				}
			}
		}).on("changed.jstree", function(e, data) {
			$('#groupId').val(data.selected);
			e.preventDefault();


			// if (empTree != undefined) {
			// 	empTree.empty();
			// }
			//$("#empTree").jstree().instance.destroy();
			// empTree = $("#empTree").jstree({
			// 	'plugins': ["wholerow",
			// 		"types"
			// 	],
			// 	'core': {
			// 		"themes": {
			// 			"responsive": false
			// 		},
			// 		'data': {
			// 			"url": ctx + "/employeeGroup/empTree?empGroupId=" + data.selected
			// 		}
			// 	},
			// 	"types": {
			// 		"default": {
			// 			"icon": "fa fa-folder icon-warning icon-lg"
			// 		},
			// 		"file": {
			// 			"icon": "fa fa-file icon-warning icon-lg"
			// 		}
			// 	}
			// })
		});
	};
	return {
		init: function() {
			editable.init();
			handleResourceTree();
		}
	};
}();